﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;

using System.Linq;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;
using EntityFramework.Extensions;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 广告
    /// </summary>
    public class AdvertDal
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model"></param>
        public void Add(Advert model)
        {
            using (Entities db = new Entities())
            {
                db.AdLists.Add(model);
                db.SaveChangesAsync().Wait();
            }

        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model"></param>
        public void Edit(Advert model)
        {
            using (Entities db = new Entities())
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }

        }
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="id"></param>
        public Advert GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                return db.AdLists.FirstOrDefault(x => x.Id == id);
            }

        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            using (Entities db = new Entities())
            {
                db.AdLists.Where(x => x.Id == id).Delete();
                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 暂停/启动广告
        /// </summary>
        /// <param name="id"></param>
        public void StopOrOpen(int id)
        {
            using (Entities db = new Entities())
            {
                var q = db.AdLists.FirstOrDefault(x => x.Id == id);
                q.IsStop = !q.IsStop;

                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 广告被点击
        /// </summary>
        /// <param name="id"></param>
        public void Click(int id)
        {
            using (Entities db = new Entities())
            {
                var q = db.AdLists.FirstOrDefault(x => x.Id == id);
                q.Clicks = q.Clicks + 1;

                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="action">类型，1正常广告，2点击最高，3点击最低，4暂停广告</param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Advert> BindList(string action, PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                DateTime date = DateTime.Now.Date;
                var query = from x in db.AdLists select x;
                switch (action)
                {
                    case null:
                        query = query
                            .Where(x => (x.MaxDateTime >= date) && (x.MaxShowTime > x.Show || x.MaxShowTime == 0) && (x.MaxClick >= x.Clicks || x.MaxClick == 0) && x.IsStop == false)
                            .OrderByDescending(x => x.Id);
                        break;
                    case "MaxClick":
                        query = query.Where(x => x.IsStop == false).OrderByDescending(x => x.Clicks);
                        break;
                    case "MinClick":
                        query = query.Where(x => x.IsStop == false).OrderBy(x => x.Clicks);
                        break;
                    case "IsStop":
                        query = query.Where(x => x.IsStop).OrderByDescending(x => x.Id);
                        break;
                    case "Invalidation":
                        query = query.Where(x => (x.MaxDateTime < date) && (x.MaxShowTime <= x.Show && x.MaxShowTime > 0) && (x.MaxClick <= x.Clicks || x.MaxClick > 0) && x.IsStop == false).OrderByDescending(x => x.Id);
                        break;
                }

                pp.RecordCount = query.Count();

                return query.Skip(pp.Skip).Take(pp.PageSize).ToList();
            }
        }
    }
}